﻿namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Contracts.Interfaces;
    using Resources;
    using System.Linq;

    /// <summary>
    /// Maximum Accumulator amount feature class
    /// </summary>
    /// <typeparam name="T">double</typeparam>
    
    public sealed class AccumulatorCapacity<T> : FeatureBase<object>
    {
        public AccumulatorCapacity(){}

        public AccumulatorCapacity(T value) 
            : base(value){ }

        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Accumulator Capacity"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup{ get { return FeatureGroupsEnum.Engine; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.AccumulatorCapacity; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_AP; } }

        /// <summary>
        /// Determines if the feature value is rounded
        /// </summary>
        public override bool IsRounded
        {
            get { return true; }
        }

        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (agent == null || robot == null) return;
            
            double temp = (double) Value *
                // Extensions
                (1 + agent.Extensions.SumModifiersValues(FeatureTypesEnum.Robot, this.FeatureType))
                // Auxiliary accumulator modules
                + robot.Modules
                    .Where(m => m.Group == ModuleGroupsEnum.AuxiliaryAccumulators && m.Modifiers != null && m.Modifiers.Any())
                    .Select(module => module.Modifiers.Where(m => m.ModifiedFeatureType == FeatureTypesEnum.Robot && m.Feature == this.FeatureType))
                    .Where(mods => mods.Any())
                    .Sum(mods => mods.Sum(modifier => modifier.Value));
            
            ModifiedValue = IsRounded
                                ? Math.Round(temp, 0, MidpointRounding.AwayFromZero)
                                : Math.Round(temp, 2);
        }
    }
}